home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / liveusb / utils.py < prev   
Encoding:
Python Source  |  2012-02-24  |  3.2 KB  |  98 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. import os
  4. import subprocess
  5. import shutil
  6. import stat
  7. import sys
  8. from liveusb import _
  9.  
  10. def _to_unicode(obj, encoding='utf-8'):
  11.     if hasattr(obj, 'toUtf8'): # PyQt4.QtCore.QString
  12.         obj = str(obj.toUtf8())
  13.     if isinstance(obj, basestring):
  14.         if not isinstance(obj, unicode):
  15.             obj = unicode(obj, encoding)
  16.     return obj
  17.  
  18. def unicode_to_utf8(string):
  19.     if isinstance(string, unicode):
  20.         return string.encode('utf-8')
  21.     return string
  22.  
  23. def extract_file_content_from_iso(iso_path, path):
  24.     """ Return the content of that file read from inside self.iso """
  25.  
  26.     cmd = ['isoinfo', '-R', '-i', unicode_to_utf8(iso_path),
  27.            '-x', unicode_to_utf8(path)]
  28.  
  29.     proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  30.     out, err = proc.communicate()
  31.     out = unicode_to_utf8(out)
  32.     err = unicode_to_utf8(err)
  33.     if proc.returncode:
  34.         raise Exception(_("There was a problem executing `%s`."
  35.                           "%s\n%s" % (cmd, out, err)))
  36.     return out
  37.  
  38. def iso_is_live_system(iso_path):
  39.     """ Return true iff a Live system is detected inside the iso_path file """
  40.     if sys.platform == 'win32':
  41.         return True
  42.     version = extract_file_content_from_iso(iso_path, '/.disk/info')
  43.     return version.startswith('Debian GNU/Linux')
  44.  
  45. def _dir_size(source):
  46.     total_size = os.path.getsize(source)
  47.     for item in os.listdir(source):
  48.         itempath = os.path.join(source, item)
  49.         if os.path.isfile(itempath):
  50.             total_size += os.path.getsize(itempath)
  51.         elif os.path.isdir(itempath):
  52.             total_size += _dir_size(itempath)
  53.     return total_size
  54.  
  55. def _move_if_exists(src, dest):
  56.     if os.path.exists(src):
  57.         shutil.move(src, dest)
  58.  
  59. def _unlink_if_exists(path):
  60.     if os.path.exists(path):
  61.         os.unlink(path)
  62.  
  63. def _set_liberal_perms_recursive(destination):
  64.     def _set_liberal_perms(arg, dirname, fnames):
  65.         if dirname == 'lost+found':
  66.             return
  67.         os.chmod(dirname, 0755)
  68.         for f in fnames:
  69.             if f == 'lost+found':
  70.                 continue
  71.             file = os.path.join(dirname, f)
  72.             if os.path.isdir(file):
  73.                 os.chmod(file, 0755)
  74.             elif os.path.isfile(file):
  75.                 os.chmod(file, 0644)
  76.     os.path.walk(destination, _set_liberal_perms, None)
  77.  
  78. def get_udisks_obj(bus):
  79.     return bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
  80.  
  81. def underlying_physical_device(path):
  82.     """ Returns the physical block device UDI (e.g.
  83.     /org/freedesktop/UDisks/devices/sdb) on which the specified file is stored.
  84.     """
  85.     import dbus
  86.     bus         = dbus.SystemBus()
  87.     rawdev      = os.stat(path)[stat.ST_DEV]
  88.     udisks_obj  = get_udisks_obj(bus)
  89.     udisks      = dbus.Interface(udisks_obj, "org.freedesktop.UDisks")
  90.     dev_path    = udisks.FindDeviceByMajorMinor(os.major(rawdev), os.minor(rawdev))
  91.     dev_obj     = bus.get_object("org.freedesktop.UDisks", dev_path)
  92.     dev         = dbus.Interface(dev_obj, "org.freedesktop.DBus.Properties")
  93.     parent_path = dev.Get(dev_path, 'PartitionSlave')
  94.     if parent_path and parent_path != '/':
  95.         return parent_path
  96.     else:
  97.         return dev_path
  98.